package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.flag.ResourcesPath;
	import engine.logic.AlertLogic;
	import engine.net.datatype.WString;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.UIManage;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.radio.NxRadioGroup;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;
	
	/**
	 *下注 
	 * @author yanghr
	 * 
	 */
	public class StrideChipInLogic
	{
		/**
		 *枚举常量 
		 */		
		private const PER_1:int=0,PER_2:int=2;
		private static var _self:StrideChipInLogic;
		public static function getInstance():StrideChipInLogic
		{
			if(_self==null)
			{
				_self=new StrideChipInLogic();
				_self.Init();
			}
			return _self;
		}
		private var _ui:StrideChipInUI;
		public function get ui():StrideChipInUI
		{
			if(_ui==null)
			{
				_ui=new StrideChipInUI;
				UIManage.getInstance().initUI(_ui,"StrideChipInUI");
			}
			return _ui;
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		
		
		private var _perGroup:NxRadioGroup;
		private var _moneyGroup:NxRadioGroup;
		private var _per1:Object;
		private var _per2:Object;
		private var _round:int;
		private var _type:int;
		private var _index:int
		
		private var _moneyMap:Object={};
		public function  Init():void
		{
			ui;
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			
			_perGroup=new NxRadioGroup([_ui.seletPerRadio1,_ui.seletPerRadio2]);
			_moneyGroup=new NxRadioGroup([_ui.moneyrRadio1,_ui.moneyrRadio2,_ui.moneyrRadio3]);
			_perGroup.selectedIndex=_moneyGroup.selectedIndex=0;
		}
		
		public function	BeforeShow():void
		{
			addAllEventListener()
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
		}
		public function addAllEventListener():void
		{
			for(var i:int=1;i<4;i++)
			{
				(_ui["moneyLab" + i ] as NxLabel).addEventListener(MouseEvent.CLICK,onLabelClick);
				if(i<3)
					(_ui["per" + i ] as NxLabel).addEventListener(MouseEvent.CLICK,onLabelClick);
			}
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.closeBtn.addEventListener(MouseEvent.CLICK,sendChipIn)
		}
		private function onLabelClick(e:MouseEvent):void
		{
			var selectLabel:NxLabel	= e.currentTarget as NxLabel;
			_ui.moneyrRadio1.selected	=	_ui.moneyrRadio2.selected	=	_ui.moneyrRadio3.selected	= false;
			switch(selectLabel)
			{
				case _ui.moneyLab1:
					_moneyGroup.selectdNxRadio	=	_ui.moneyrRadio1;
					break;
				case _ui.moneyLab2:
					_moneyGroup.selectdNxRadio	=	_ui.moneyrRadio2;
					break;
				case _ui.moneyLab3:
					_moneyGroup.selectdNxRadio	=	_ui.moneyrRadio3;
					break;
				case _ui.per1:
					_perGroup.selectdNxRadio=_ui.seletPerRadio1;
					break
				case _ui.per2:
					_perGroup.selectdNxRadio=_ui.seletPerRadio2;
					break;
			}
		}
		public function removeAllEventListener():void
		{
			for(var i:int=1;i<4;i++)
			{
				(_ui["moneyLab" + i ] as NxLabel).removeEventListener(MouseEvent.CLICK,onLabelClick);
			}
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.closeBtn.removeEventListener(MouseEvent.CLICK,sendChipIn)
		}
		private function close(e:Event=null):void
		{
			_ui.hide();
		}
		/**
		 *主入口  
		 * @param per1
		 * @param per2
		 * @param round	场
		 * @param type	天邦or地榜
		 * @param index	第几场
		 * 
		 */		
		public function showWithPer(per1:Object,per2:Object,round:int,type:int,index:int):void
		{
			_index=index;
			_type=type;
			this.per1=per1;
			this.per2=per2;
			this.round=round+1;
			if(!_ui.created)
			{
				_ui.showOrHide();
			}
		}
		private function set round(value:int):void 
		{
			_round=value;
			var level:int=_role.getPlayerLevel();
			var str:String = configHead +value;
			var config:Object=StaticIni.getIniObj(str);
			for (var i:String in config) 
			{
				var arr:Array=i.split("-");
				if(level>=arr[0]&&level<=arr[1])
				{
					moneyString=config[i];
					return 
				}
			}
		}
		private function get configHead():String
		{
			if(_type==StrideMainLogic.SKY_LOCAL||_type==StrideMainLogic.GROUND_LOCAL)
				return "Local"
			return "Cross"
		}
		private function set moneyString(value:String):void
		{
			var arr:Array=value.split(",");
			for (var i:int = 0; i < arr.length; i++) 
			{
				setMoney(i+1,int(arr[i]))
			}
		}
		
		private function setMoney(index:int,money:int):void
		{
			_ui["moneyLab"+index].htmlText=StaticIni.getSwfLangStrVar("Stride_14",[money]);
			_moneyMap[index]=money
		}
		public function set per1(value:Object):void 
		{
			_per1=value;
			setPhoto(PER_1,value[ActorPropertyUI.MODEL],value[ActorPropertyUI.CLOTHE_MODEL]);
			setLevel(PER_1,value[ActorPropertyUI.LEVEL]);
			setName(PER_1,value[ActorPropertyUI.NAME]);
			setRepute(PER_1,value[ActorPropertyUI.REPUTATION])
			setFightPower(PER_1,value[ActorPropertyUI.FIGHT_POWER]);
		}
		public function set per2(value:Object):void 
		{
			_per2=value;
			setPhoto(PER_2,value[ActorPropertyUI.MODEL],value[ActorPropertyUI.CLOTHE_MODEL]);
			setLevel(PER_2,value[ActorPropertyUI.LEVEL]);
			setName(PER_2,value[ActorPropertyUI.NAME]);
			setRepute(PER_2,value[ActorPropertyUI.REPUTATION])
			setFightPower(PER_2,value[ActorPropertyUI.FIGHT_POWER]);
		}
		private function setPhoto(enumType:int,modelID:int,clotheID:int=0):void
		{
			trace("StrideChipInLogic.setPhoto(enumType, modelID, clotheID)",modelID,clotheID);
			const PIC:String="pic";
			var mc:MovieClip=enumType==PER_1?_ui.facBG1:_ui.facBG2;
			if(!mc[PIC])
			{
				var nx:NxClip=new NxClip;
				mc["img01"].addChild(nx);
				mc[PIC]=nx;
			}
			mc[PIC].source=ResourcesPath.getHeadPic(modelID,clotheID);
		}
		private function setName(enumType:int,name:String):void
		{
			var lab:NxLabel=enumType==PER_1?_ui.nameLab1:_ui.nameLab2;
			lab.htmlText=name;
		}
		private function setRepute(enumType:int,repute:int):void
		{
			var lab:NxLabel=enumType==PER_1?_ui.ReputeLab1:_ui.ReputeLab2;
			lab.htmlText=StaticIni.getSwfLangStrVar("StrideChip_8",[repute]);
		}
		private function setLevel(enumType:int,level:int):void
		{
			var lab:NxLabel=enumType==PER_1?_ui.lvlLab1:_ui.lvlLab2;
			lab.htmlText=StaticIni.getSwfLangStrVar("StrideChip_3",[level]);//level;
		}
		private function setFightPower(enumType:int,level:int):void
		{
			var lab:NxLabel=enumType==PER_1?_ui.fightCountLab1:_ui.fightCountLab2;
			lab.htmlText=StaticIni.getSwfLangStrVar("StrideChip_9",[level]);//level;
		}
		private function clickCallBack(type:int,str:String,clickData:*):void
		{
			if(AlertUI.YES == type)//同意
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_RISE_PLEDGE,_type,_index,new WString(clickData[0]),clickData[1]);
			}
		}
		private function sendChipIn(e:Event):void
		{
			
			trace("发送下注消息");
			var money:int=_moneyMap[_moneyGroup.selectedIndex+1];
			var seletted:int=_perGroup.selectedIndex;
			if(seletted==-1)
			{
				trace( "请先下注");
				return
			}
			var name:String=seletted==0?_per1[ActorPropertyUI.NAME]:_per2[ActorPropertyUI.NAME];
			AlertUI.showAlert("",StaticIni.getSwfLangStrVar("UIString_1000597",[name,money]),AlertUI.YES|AlertUI.NO,clickCallBack,[name,money])
			//			//跨服战晋级赛押注
			//			CLIENT_CUSTOMMSG_RISE_PLEDGE = 20823,
			//				参数
			//				1. fightType    战场类型(天榜, 地榜)
			//				2. site         战场序号
			//			3. pledgePlayer 押注玩家
			//			4. pledgeGold    押注金额
			//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_RISE_PLEDGE,_type,_index,new WString(name),money);
			
			//			close();
		}
	}
}